import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mind_rubbish_type/utils/api.dart';
import 'package:mind_rubbish_type/utils/globalColor.dart';

class QueryResult extends StatefulWidget {
  QueryResult({Key key, @required this.keyword}) : super(key: key);
  final String keyword;
  @override
  _QueryResultState createState() => _QueryResultState();
}

class _QueryResultState extends State<QueryResult> {
  Map searchData = {'keyword': ''};
  List subListTxt = [
    '1.做好垃圾分类，人人有责',
    '2.垃圾分类，利国利民',
    '3.爱护今天的自然，自然将呵护你的明天。',
    '4.保护环境，人人有责',
    '5.地球是我们的妈妈，我们是妈妈的孩子，热爱妈妈吧！',
    '6.请别乱坎树林，让空气更加清新。',
  ];
  @override
  void initState() {
    EasyLoading.instance.loadingStyle = EasyLoadingStyle.light;
    EasyLoading.instance.maskType = EasyLoadingMaskType.black;

    EasyLoading.show(status: '搜索中');

    getSearchResult(widget.keyword).then((res) {
      print(res);
      setState(() {
        searchData = res;
      });
      EasyLoading.dismiss();
    });
    super.initState();
  }

  // 获取搜索结果
  getSearchResult(val) async {
    try {
      var response = await HttpRequest.request("/garbage/search?key=" + val,
          method: HttpRequest.GET);
      return response['data'];
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    print("ddddd${searchData}");
    return Scaffold(
        appBar: AppBar(
          title: Text(
            "搜索结果",
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              fontSize: 15,
            ),
          ),
        ),
        body: searchData['keyword'] == ''
            ? Container(
                width: MediaQuery.of(context).size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [Text('垃圾分类，人人有责，从自我做起！')],
                ))
            : Container(
                width: MediaQuery.of(context).size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    searchData['catpicList'] != null
                        ? FadeInImage.assetNetwork(
                            width: MediaQuery.of(context).size.width,
                            placeholder: 'assets/images/placeholder.jpg',
                            fadeInDuration: const Duration(milliseconds: 500),
                            image: searchData['catpicList'].length > 0
                                ? searchData['catpicList'][0]
                                : '',
                            fit: BoxFit.cover,
                          )
                        : Image.asset('assets/images/placeholder.jpg'),
                    Padding(
                      padding: EdgeInsets.only(top: 10, bottom: 10, left: 10),
                      child: Text(
                          "名称：${searchData['keyword'] == null ? '' : searchData['keyword']}",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                            fontWeight: FontWeight.w500,
                          )),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 10, bottom: 10, left: 10),
                      child: Text(
                          "标签：${searchData['recommended'] == null ? '' : searchData['recommended']}",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 18,
                            fontWeight: FontWeight.w500,
                          )),
                    ),
                    ...genSubList(subListTxt)
                  ],
                ),
              ));
  }

  // handelSearchBody
  List<Widget> genSubList(arr) {
    List<Widget> subList = [
      SizedBox(height: 10),
      Row(
        children: [
          SizedBox(width: 10),
          Padding(
              padding: EdgeInsets.only(bottom: 1),
              child: Text(
                "在此我们倡议：",
                maxLines: 5,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    fontSize: 22,
                    fontWeight: FontWeight.w700,
                    color: GlobalColor.themeColor),
              ))
        ],
      ),
      SizedBox(height: 10),
    ];
    List.generate(arr.length, (index) {
      subList.add(Padding(
        padding: EdgeInsets.only(left: 15, top: 5),
        child: Text(
          arr[index],
          maxLines: 5,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.w700,
          ),
        ),
      ));
    });
    return subList;
  }
}
